import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:my_desk_flutter/controller/auth_controller.dart';

class AuthCheck extends ConsumerWidget {
  final Widget child;
  final VoidCallback? onTap;

  const AuthCheck({
    super.key,
    required this.child,
    this.onTap,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () async {
        final isLogin = await ref.refresh(authControllerProvider.future);
        if (!isLogin.isAuth) {
          if (context.mounted) {
            Navigator.pushNamed(context, '/login');
          }
        } else {
          onTap?.call();
        }
      },
      child: IgnorePointer(
        child: child,
      ),
    );
  }
}
